# -*- coding = utf-8 -*-
# @Time: 2024/7/11 22:18
# @Author:Ethan
# @File:signalslot.py
# @Software:Pycharm
import sys
import time

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class MyWindow(QWidget):
    #声明信号 只能放在函数外边
    my_signal = pyqtSignal(str)
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        self.resize(300,300)

        container = QVBoxLayout()

        self.msg = QLabel("")
        self.msg.resize(280,15)

        self.msg.setWordWrap(True)          #自动换行
        self.msg.setAlignment(Qt.AlignTop)  #靠上
        self.msg.setStyleSheet('background-color:yellow;color:black;')

        self.msg_history = []
        #创建滚动对象
        scroll = QScrollArea()
        scroll.setWidget(self.msg)

        #垂直布局器
        v_layout = QVBoxLayout()
        v_layout.addWidget(scroll)

        #水平布局器
        h_layout = QHBoxLayout()

        btn = QPushButton('开始检测',self)
        #绑定点击
        btn.clicked.connect(self.check)
        h_layout.addStretch(1)
        h_layout.addWidget(btn)
        h_layout.addStretch(1)

        container.addLayout(v_layout)
        container.addLayout(h_layout)

        self.setLayout(container)

        #绑定信号和槽
        self.my_signal.connect(self.my_slot)


    def my_slot(self,msg):
        print(msg)
        self.msg_history.append(msg)
        self.msg.setText("<br>".join(self.msg_history))
        self.msg.resize(440,self.msg.frameSize().height()+15)
        self.msg.repaint()

    def check(self):
        for i, ip in enumerate(["192.168.1.%d" % x for x in range(1,256)]):
            msg = '模拟，正在检查%s上的漏洞 '% ip
            if i%5 == 0:
                self.my_signal.emit(msg + "【发现漏洞】")
            else:
                self.my_signal.emit(msg)
            time.sleep(0.01)


    def pyqtSingal(self,str):
        pass

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w=MyWindow()
    w.show()
    app.exec()